//
//  ViewController.swift
//  day03
//
//  Created by 杨绍鑫 on 2018/9/6.
//  Copyright © 2018年 crcnet. All rights reserved.
//

import UIKit
import AVKit
class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var data = [
        Video(image: "videoScreenshot01", title : "Introduce 3DS Mario", source:"Youtube - 06:32"),
        Video(image: "videoScreenshot02", title : "Emoji Among Us", source:"Vimeo - 3:34"),
        Video(image: "videoScreenshot03", title : "Seals Documentary", source:"Vine - 00:06"),
        Video(image: "videoScreenshot04", title : "Adventure Time", source:"Youtube - 02:39"),
        Video(image: "videoScreenshot05", title : "Facebook HQ", source:"Facebook - 10:20"),
        Video(image: "videoScreenshot06", title : "Lijiang Lugu Lake", source:"Allen - 20:30")
        ]
    
    var player = AVPlayer()
    let playerVC = AVPlayerViewController()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func playVideo()  {
        let urlStr = Bundle.main.path(forResource: "emoji zone", ofType: "mp4")
        
        player = AVPlayer.init(url:URL.init(fileURLWithPath: urlStr!))
        playerVC.player = player
        self.present(playerVC, animated: true) {
            self.player.play()
        }
    }

}
extension ViewController : UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "celll____", for: indexPath) as! VideoCell
        let video = data[indexPath.row]
        
        cell.bgImageView.image = UIImage.init(named: video.image)
        cell.videoLable.text = video.title
        cell.sourceLable.text = video.source
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        self.playVideo()
    }
    
}

